package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 17, 2005
 * Time  : 10:54:28 AM
 */
public class PurchaseOrderGenerateCommand extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        PurchaseOrderForm purchaseOrderForm = ( PurchaseOrderForm ) form;
		String actionForward 		= "Generate";
		HttpSession mSession 		= request.getSession();

		try
        {
            //Loading the Vendors of Fab, Sort, Assembly and Test.
            request.setAttribute( "vendorsList", PurchaseOrderDAO.loadPOVendors() );

            //Loading the Purchase Order Types.
            request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
            
			purchaseOrderForm.setVendor_id( -1 );
			purchaseOrderForm.setPoType( 1 );

            actionForward = "Generate";
       	}
        catch ( Exception e )
        {
            Logger.error( "Could Not Load the Vendor List", e );
            errors.put( "Could Not Load the Vendor List", null );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
            actionForward = "Generate";
        }

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}

}
